﻿using System.Net;
using System.Text.RegularExpressions;

namespace Toybox.NicoSharp.Seiga
{
	using Utilities;

	public partial class Book : NicoSharp.Abstracts.AbstractXmlContent
	{
		// UNDONE: BookPlayer
		// POST - http://bkapi.seiga.nicovideo.jp/user/last_read
		// POST - http://bkdrm01.nicoseiga.jp/maki/drm

		#region [Static]

		public const string REGEX_RECOMMENDATION = "(?<inner>{.*})";

		#endregion [Static]


		#region Constructor

		public Book(ContentId contentId, CookieCollection cookies = null)
			: base(contentId, cookies)
		{
		}

		public Book(string id, CookieCollection cookies = null)
			: base(id, cookies)
		{
		}

		#endregion Constructor


		#region Public Methods

		public Recommendation GetRecommendation()
		{
			string url = NicoUrls.BookApi + "book/" + this.ContentId.Digits + "/recommendation/jsonp?callback=jQuery";
			string json = NicoWebClient.Get(url, this.Cookies);

			Match match = Regex.Match(json, REGEX_RECOMMENDATION);
			if (match.Success)
			{
				return Deserializer.JsonDeserializer<Recommendation>(match.Groups["inner"].Value);
			}

			return null;
		}

		public BookInfo GetBookInfo()
		{
			string url = NicoUrls.BookApi + "book/" + this.ContentId.Digits;
			string json = NicoWebClient.Get(url, this.Cookies);
			return Deserializer.JsonDeserializer<BookInfo>(json);
		}

		public Download GetDownload()
		{
			string url = NicoUrls.BookApi + "book/" + this.ContentId.Digits + "/download";
			string json = NicoWebClient.Get(url, this.Cookies);
			return Deserializer.JsonDeserializer<Download>(json);
		}

		public LastRead GetLastRead()
		{
			string url = NicoUrls.BookApi + "user/last_read?book_id=" + this.ContentId.Digits;
			string json = NicoWebClient.Get(url, this.Cookies);
			return Deserializer.JsonDeserializer<LastRead>(json);
		}

		#endregion Public Methods

	}
}
